package cn.edu.xjtu.android.emoji_warfare_android.game;

import java.util.Random;

import cn.edu.xjtu.android.emoji_warfare_android.engine.worker.DelayedOperation;
import cn.edu.xjtu.android.emoji_warfare_android.game.hero.Player;

/**
 * AutoShoot<br>
 * 自动射击
 * 
 * @author wyyf
 *
 */
public class AutoShoot extends DelayedOperation {

    /**
     * 玩家主角引用
     */
    Player p;
    /**
     * 线程状态
     */
    boolean isAlive = true;

    public AutoShoot(Player e, int t) {
        this.p = e;
        this.setDelayMillis(t);
    }

    public void interrupt() {
        isAlive = false;
    }

    public void resume() {
        isAlive = true;
    }

    @Override
    public boolean run() {
        if (isAlive) {
            // 到达时间关闭武器
            if (Engine.instance.clock() > p.getWeaponOverTime()) {
                p.setWeapon(0);
                p.stopLaser();
            }
            // 防止死亡过程无法关闭激光
            if (p.getWeapon() != 4) {
                p.stopLaser();
            }
            // 根据玩家的武器确定发射何种子弹
            switch (p.getWeapon()) {
            case 0:
                p.fire();
                break;
            case 1:
                p.fireScattershot();
                break;
            case 2:
                p.fireParallel();
                break;
            case 3:
                p.fireLine();
                break;
            case 4:
                break;
            }
        }
        return false;
    }

    public final static Random r;
    static {
        r = new Random(System.currentTimeMillis());
    }

}
